package cn.ekuma.epos.datalogic.define.dao;

import java.util.Date;
import java.util.List;

import e.odbo.data.dao.BaseDAO;
import cn.ekuma.epos.db.table.I_PlaceState;
import com.openbravo.bean.PlaceState;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.serialize.DataRead;
import com.openbravo.data.loader.serialize.DataWrite;
import com.openbravo.data.loader.serialize.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.key.KeyBuilder;
import com.openbravo.data.loader.table.TableDefinition;
import com.openbravo.data.loader.query.QBFParameter;
import com.openbravo.data.loader.query.QBFParameters;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;

public class PlaceStateDAO extends BaseDAO<PlaceState>{
	
	public PlaceStateDAO(I_Session s) {
		super(s);
	}

	 @Override
	    public TableDefinition getTable() {
	        return new TableDefinition(
	                "PLACE_STATE",
	                new Field[]{
	                    new Field(I_PlaceState.ID, Datas.STRING, Formats.STRING),
	                    new Field(I_PlaceState.PLACES_ID, Datas.STRING, Formats.STRING),
	                    new Field(I_PlaceState.GROUPNAME, Datas.STRING, Formats.STRING),
	                    new Field(I_PlaceState.CAPACITY, Datas.INT, Formats.INT),
	                    new Field(I_PlaceState.MANAGER, Datas.STRING, Formats.STRING),
	                    new Field(I_PlaceState.ADVANCE_PRICE, Datas.DOUBLE, Formats.DOUBLE),
	                    new Field(I_PlaceState.PLACESTATE, Datas.STRING, Formats.STRING),
	                    new Field(I_PlaceState.BEGIN_TIME, Datas.TIMESTAMP, Formats.TIMESTAMP),
	                    new Field(I_PlaceState.CHANGE_TIME, Datas.TIMESTAMP, Formats.TIMESTAMP),
	                    new Field(I_PlaceState.LASTMODIFIED, Datas.TIMESTAMP, Formats.TIMESTAMP)
	                },
	                new int[]{0});
	    }

	    @Override
	    public void writeInsertValues(DataWrite dp, PlaceState obj) throws BasicException {
	       dp.setString(1, obj.getId());
	       dp.setString(2, obj.getPlaceId());
	       dp.setString(3, obj.getM_sGroupName());
	       dp.setInt(4, obj.getCapacity());
	       dp.setString(5, obj.getManager());
	       dp.setDouble(6,obj.getAdvancePrice());
	       dp.setString(7,obj.getM_sPlaceState());
	       dp.setTimestamp(8,obj.getBeginTime());
	       dp.setTimestamp(9,obj.getChangeTime());
	       dp.setTimestamp(10,obj.getLastModifier());
	    }


	    public PlaceState readValues(DataRead dr,PlaceState obj) throws BasicException {
	    	if(obj==null)
	    		obj=new PlaceState();
	    	obj.setId(dr.getString(1));
	    	obj.setPlaceId(dr.getString(2));
	    	obj.setM_sGroupName(dr.getString(3));
	    	obj.setCapacity(dr.getInt(4));
	    	obj.setManager(dr.getString(5));
	    	obj.setAdvancePrice(dr.getDouble(6));
	    	obj.setM_sPlaceState(dr.getString(7));
	    	obj.setBeginTime(dr.getTimestamp(8));
	    	obj.setChangeTime(dr.getTimestamp(9));
	    	obj.setLastModifier(dr.getTimestamp(10));
	        return obj;
	    }
	    
	    @Override
	    public Class getSuportClass() {
	        return PlaceState.class;
	    }

	    public void updatePlaceState(String id,String newState)throws BasicException {
	    	updateByFields(KeyBuilder.getKey(id), new String[]{I_PlaceState.PLACESTATE,I_PlaceState.CHANGE_TIME}, new Object[]{newState,new Date()});
	    }
	    
	    public void joinGroup(String id,String group)throws BasicException {
	    	updateByFields(KeyBuilder.getKey(id), new String[]{I_PlaceState.GROUPNAME,I_PlaceState.CHANGE_TIME}, new Object[]{group,new Date()});
	    }
	    
	    QBFParameter timeAfter=new QBFParameter(I_PlaceState.CHANGE_TIME);
	    QBFParameter timeBef=new QBFParameter(I_PlaceState.CHANGE_TIME);
	    QBFParameters paras=new QBFParameters().and(timeBef).and(timeAfter);
	    
	    public List<PlaceState> loadPlacesStateBetween(Date start,Date afterTime)throws BasicException {
	    	timeAfter.less(afterTime);
	    	timeBef.greaterOrEqual(start);
	        return query(paras);
	    }

        public void setPlaceManager(String id,String manager)throws BasicException {
        	updateByFields(KeyBuilder.getKey(id), new String[]{I_PlaceState.MANAGER,I_PlaceState.CHANGE_TIME}, new Object[]{manager,new Date()});
        }
}
